//
//  AppDelegate.m
//  ZLFastDoctor
//
//  Created by 李宗帅 on 16/5/21.
//  Copyright © 2016年 LS. All rights reserved.
//
#import "XZGRegisterViewController.h"
#import "AppDelegate.h"
#import "NQHomeVc.h"
#import "NQLeftMenuVc.h"
#import "NQNavVc.h"
#import <SMS_SDK/SMSSDK.h>
#import "HMSaveDataTool.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
#import "LSNewfeatureViewController.h"

//腾讯开放平台（对应QQ和QQ空间）SDK头文件
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>

//微信SDK头文件
#import "WXApi.h"

//新浪微博SDK头文件
#import "WeiboSDK.h"
//新浪微博SDK需要在项目Build Settings中的Other Linker Flags添加"-ObjC"

#import "OKDoctorInfoController.h"

// 即时通讯
#import <RongIMKit/RongIMKit.h>
#import <RongIMLib/RongIMLib.h>

// App-Key
#define RONGCLOUD_IM_APPKEY @"3argexb6rkfse"
#import "MORootNavigationController.h"

//友盟统计
#import <UMMobClick/MobClick.h>


@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 下面是即时通讯相关功能
    [[RCIM sharedRCIM] initWithAppKey:RONGCLOUD_IM_APPKEY];
    
    /**
     * 推送处理1
     */
    if ([application
         respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        //注册推送, iOS 8
        UIUserNotificationSettings *settings = [UIUserNotificationSettings
                                                settingsForTypes:(UIUserNotificationTypeBadge |
                                                                  UIUserNotificationTypeSound |
                                                                  UIUserNotificationTypeAlert)
                                                categories:nil];
        [application registerUserNotificationSettings:settings];
    } else {
        UIUserNotificationType myTypes = UIUserNotificationTypeBadge |
        UIUserNotificationTypeAlert |
        UIUserNotificationTypeSound;
        [application registerForRemoteNotificationTypes:myTypes];
    }
    // 上面是即时通讯部分
    
    [SMSSDK registerApp:@"f4a0ff39df13" withSecret:@"cc7c40e31e1421eae4d46243c8a5ee12"];
    
    //1.创建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = NQColor(240, 238 , 246);
    
    //2.设置根控制器
    //左边侧滑菜单控制器
    self.leftMenuVc = [[NQLeftMenuVc alloc] init];
    //右边中心控制器
    NQHomeVc *homeVc = [[NQHomeVc alloc] init];
    NQNavVc *navCenterVc = [[NQNavVc alloc] initWithRootViewController:homeVc];
    self.drawer = [[ICSDrawerController alloc] initWithLeftViewController:self.leftMenuVc centerViewController:navCenterVc];
    
    //登录控制器
    XZGRegisterViewController *registerVc = [[XZGRegisterViewController alloc] init];
    MORootNavigationController *navVc = [[MORootNavigationController alloc] initWithRootViewController:registerVc];
    navVc.navigationBar.backgroundColor = NQNavColor;
    
    //保存版本号使用的Key
    NSString *versonKey = @"CFBundleVersion";
    
    //获取上次的版本号
    
    NSString *lastVersion = [HMSaveDataTool valueForKey:versonKey];
    
    //获取当前的版本号
    NSDictionary *infoData = [NSBundle mainBundle].infoDictionary;
    NSString *currentVersion = infoData[versonKey];
    
    
    if ([currentVersion isEqualToString:lastVersion]) {
        
        //设置根控制器为登录控制器
        self.window.rootViewController = navVc;
    } else {
        LSNewfeatureViewController *newsVc = [[LSNewfeatureViewController alloc] init];
        // 设置根控制器
        self.window.rootViewController = newsVc;
        // 保存版本号
        [HMSaveDataTool setValue:currentVersion forKey:versonKey];
    }
    
    //3.显示窗口
    [self.window makeKeyAndVisible];
    
    /**
     *  友盟分享
     */
    
    UMConfigInstance.appKey = @"5747f54c67e58e33ed0000ce";
    UMConfigInstance.channelId = nil;
    [MobClick startWithConfigure:UMConfigInstance];
    [MobClick setLogEnabled:YES];//开启Debug模式,用于查看Log
    
    
    /**
     *  设置ShareSDK的appKey，如果尚未在ShareSDK官网注册过App，请移步到http://mob.com/login 登录后台进行应用注册，
     *  在将生成的AppKey传入到此方法中。
     *  方法中的第二个第三个参数为需要连接社交平台SDK时触发，
     *  在此事件中写入连接代码。第四个参数则为配置本地社交平台时触发，根据返回的平台类型来配置平台信息。
     *  如果您使用的时服务端托管平台信息时，第二、四项参数可以传入nil，第三项参数则根据服务端托管平台来决定要连接的社交SDK。
     */
    [ShareSDK registerApp:@"iosv1101"
     
          activePlatforms:@[
                            @(SSDKPlatformTypeSinaWeibo),
                            @(SSDKPlatformTypeMail),
                            @(SSDKPlatformTypeSMS),
                            @(SSDKPlatformTypeWechat),
                            @(SSDKPlatformTypeQQ)]
                 onImport:^(SSDKPlatformType platformType)
     {
         switch (platformType)
         {
             case SSDKPlatformTypeWechat:
                 [ShareSDKConnector connectWeChat:[WXApi class]];
                 break;
             case SSDKPlatformTypeQQ:
                 [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
                 break;
             case SSDKPlatformTypeSinaWeibo:
                 [ShareSDKConnector connectWeibo:[WeiboSDK class]];
                 break;
             default:
                 break;
         }
     }
          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
     {
         
         switch (platformType)
         {
             case SSDKPlatformTypeSinaWeibo:
                 //设置新浪微博应用信息,其中authType设置为使用SSO＋Web形式授权
                 [appInfo SSDKSetupSinaWeiboByAppKey:@"568898243"
                                           appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3"
                                         redirectUri:@"http://www.sharesdk.cn"
                                            authType:SSDKAuthTypeBoth];
                 break;
             case SSDKPlatformTypeWechat:
                 [appInfo SSDKSetupWeChatByAppId:@"wx4868b35061f87885"
                                       appSecret:@"64020361b8ec4c99936c0e3999a9f249"];
                 break;
             case SSDKPlatformTypeQQ:
                 [appInfo SSDKSetupQQByAppId:@"100371282"
                                      appKey:@"aed9b0303e3ed1e27bae87c33761161d"
                                    authType:SSDKAuthTypeBoth];
                 break;
             default:
                 break;
         }
     }];
    
    
    // 设置消息监听,即时通讯
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(didReceiveMessageNotification:)
     name:RCKitDispatchMessageNotification
     object:nil];
    [[RCIM sharedRCIM] setConnectionStatusDelegate:self];
    
    
    return YES;
}

/**
 *  将得到的devicetoken 传给融云用于离线状态接收push ，您的app后台要上传推送证书
 */
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString *token =
    [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"
                                                           withString:@""]
      stringByReplacingOccurrencesOfString:@">"
      withString:@""]
     stringByReplacingOccurrencesOfString:@" "
     withString:@""];
    
    [[RCIMClient sharedRCIMClient] setDeviceToken:token];
}


/**
 *  网络状态变化。
 *
 *  @param status 网络状态。
 */
- (void)onRCIMConnectionStatusChanged:(RCConnectionStatus)status {
    if (status == ConnectionStatus_KICKED_OFFLINE_BY_OTHER_CLIENT) {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"提示"
                              message:@"您"
                              @"的帐号在别的设备上登录，您被迫下线！"
                              delegate:nil
                              cancelButtonTitle:@"知道了"
                              otherButtonTitles:nil, nil];
        [alert show];
//        ViewController *loginVC = [[ViewController alloc] init];
//        UINavigationController *_navi =
//        [[UINavigationController alloc] initWithRootViewController:loginVC];
//        self.window.rootViewController = _navi;
    }
}

// 通知图标
- (void)didReceiveMessageNotification:(NSNotification *)notification {
//    [UIApplication sharedApplication].applicationIconBadgeNumber =
//    [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
}


@end
